/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

{{! Select all the comment items and expand them here }}
{{#.}}
{{#comment}}
{{comment}}
{{/comment}}
{{/.}}

#include "presto_cpp/main/thrift/ProtocolToThrift.h"
#include "presto_cpp/main/thrift/ThriftIO.h"
#include "presto_cpp/presto_protocol/core/ConnectorProtocol.h"

namespace facebook::presto::thrift {

// These could be covered by a more general template but this way only
// conversions to supported Thrift data types can be generated.
void toThrift(const std::string& proto, std::string& thrift) {
  thrift = proto;
}
void toThrift(const bool& proto, bool& thrift) {
  thrift = proto;
}
void toThrift(const int16_t& proto, int16_t& thrift) {
  thrift = proto;
}
void toThrift(const int32_t& proto, int32_t& thrift) {
  thrift = proto;
}
void toThrift(const int64_t& proto, int64_t& thrift) {
  thrift = proto;
}
void toThrift(const double& proto, double& thrift) {
  thrift = proto;
}
void toThrift(const facebook::presto::protocol::Duration& duration, double& thrift) {
  thrift = duration.getValue(facebook::presto::protocol::TimeUnit::MILLISECONDS);
}
void toThrift(const facebook::presto::protocol::DataSize& dataSize, double& thrift) {
  thrift = dataSize.getValue(facebook::presto::protocol::DataUnit::BYTE);
}

template <typename P, typename T>
void toThrift(const std::shared_ptr<P>& proto, std::shared_ptr<T>& thrift) {
  if (proto) {
    thrift = std::make_shared<T>();
    toThrift(*proto, *thrift);
  }
}

template <typename P, typename T>
void toThrift(const std::shared_ptr<P>& proto, T& thrift) {
  if (proto) {
    toThrift(*proto, thrift);
  }
}

template <typename V, typename S>
void toThrift(const std::vector<V>& v, std::set<S>& s) {
  S toItem;
  for (const auto& fromItem : v) {
    toThrift(fromItem, toItem);
    s.insert(std::move(toItem));
  }
}

template <typename P, typename T>
void toThrift(const std::vector<P>& p, std::vector<T>& t) {
  t.reserve(p.size());
  T toItem;
  for (const auto& fromItem : p) {
    toThrift(fromItem, toItem);
    t.emplace_back(std::move(toItem));
  }
}

template <typename K1, typename V1, typename K2, typename V2>
void toThrift(const std::map<K1, V1>& protoMap, std::map<K2, V2>& thriftMap) {
  K2 toKey;
  V2 toValue;
  for (const auto& [fromKey, fromValue] : protoMap) {
    toThrift(fromKey, toKey);
    toThrift(fromValue, toValue);
    thriftMap.emplace(std::move(toKey), std::move(toValue));
  }
}

template <typename P, typename T>
void toThrift(const std::shared_ptr<P>& proto, apache::thrift::optional_field_ref<T> thrift) {
  if (proto) {
    thrift.ensure();
    toThrift(*proto, apache::thrift::can_throw(*thrift));
  }
}

void fromThrift(const std::string& thrift, std::string& proto) {
  proto = thrift;
}
void fromThrift(const bool& thrift, bool& proto) {
  proto = thrift;
}
void fromThrift(const int16_t& thrift, int16_t& proto) {
  proto = thrift;
}
void fromThrift(const int32_t& thrift, int32_t& proto) {
  proto = thrift;
}
void fromThrift(const int64_t& thrift, int64_t& proto) {
  proto = thrift;
}
void fromThrift(const double& thrift, double& proto) {
  proto = thrift;
}

void fromThrift(const double& thrift, facebook::presto::protocol::Duration& duration) {
  duration = facebook::presto::protocol::Duration(thrift, facebook::presto::protocol::TimeUnit::MILLISECONDS);
}

void fromThrift(const double& thrift, facebook::presto::protocol::DataSize& dataSize) {
  dataSize = facebook::presto::protocol::DataSize(thrift, facebook::presto::protocol::DataUnit::BYTE);
}

template <typename P, typename T>
void fromThrift(const apache::thrift::optional_field_ref<T>& thrift, std::shared_ptr<P>& proto) {
  if (thrift.has_value()) {
    proto = std::make_shared<P>();
    fromThrift(*thrift, *proto);
  }
}

template <typename P, typename T>
void fromThrift(const T& thrift, std::shared_ptr<P>& proto) {
  proto = std::make_shared<P>();
  fromThrift(thrift, *proto);
}

template <typename P, typename T>
void fromThrift(const std::shared_ptr<P>& thrift, std::shared_ptr<T>& proto) {
  if (thrift) {
    proto = std::make_shared<T>();
    fromThrift(*thrift, *proto);
  }
}

template <typename V, typename S>
void fromThrift(const std::set<S>& thrift, std::vector<V>& proto) {
  proto.reserve(thrift.size());
  V toItem;
  for (const auto& fromItem : thrift) {
    fromThrift(fromItem, toItem);
    proto.emplace_back(std::move(toItem));
  }
}

template <typename P, typename T>
void fromThrift(const std::vector<P>& thrift, std::vector<T>& proto) {
  proto.reserve(thrift.size());
  T toItem;
  for (const auto& fromItem : thrift) {
    fromThrift(fromItem, toItem);
    proto.emplace_back(std::move(toItem));
  }
}

template <typename K1, typename V1, typename K2, typename V2>
void fromThrift(const std::map<K1, V1>& thriftMap, std::map<K2, V2>& protoMap) {
  K2 toKey;
  V2 toValue;
  for (const auto& [fromKey, fromValue] : thriftMap) {
    fromThrift(fromKey, toKey);
    fromThrift(fromValue, toValue);
    protoMap.emplace(std::move(toKey), std::move(toValue));
  }
}

{{! Select all the items and expand either the "hinc" member or the "struct", "enum" members }}
{{#.}}
{{#cinc}}
{{&cinc}}
{{/cinc}}
{{^cinc}}
{{#connector}}
void toThrift(const std::shared_ptr<facebook::presto::protocol::{{class_name}}>& proto, {{class_name}}& thrift) {
}
void fromThrift(const {{class_name}}& thrift, std::shared_ptr<facebook::presto::protocol::{{class_name}}>& proto) {
  if (thrift.connectorId().has_value() && thrift.customSerializedValue().has_value()) {
    facebook::presto::protocol::getConnectorProtocol(thrift.connectorId().value())
      .deserialize(thrift.customSerializedValue().value(), proto);
  } else if (thrift.jsonValue().has_value()) {
    json j = json::parse(thrift.jsonValue().value());
    from_json(j, proto);
  }
}

{{/connector}}
{{#union}}
void toThrift(
    const std::shared_ptr<facebook::presto::protocol::{{proto_name}}>& proto,
    apache::thrift::optional_field_ref<{{class_name}}&> thrift) {
  if (proto) {
    thrift.ensure();
    toThrift(proto, *thrift);
  }
}
void toThrift(
    const std::shared_ptr<facebook::presto::protocol::{{proto_name}}>& proto,
    {{class_name}}& thrift) {
  {{#fields}}
  if (auto {{field_name}} =
    std::dynamic_pointer_cast<facebook::presto::protocol::{{field_type}}>(proto)) {
    {{field_type}} thrift{{field_type}};
    toThrift(*{{field_name}}, thrift{{field_type}});
    thrift.set_{{field_name}}(std::move(thrift{{field_type}}));
  }
  {{/fields}}
}
void fromThrift(
    apache::thrift::optional_field_ref<const {{class_name}}&> thrift,
    std::shared_ptr<facebook::presto::protocol::{{proto_name}}>& proto) {
  if (thrift.has_value()) {
    proto = std::make_shared<facebook::presto::protocol::{{proto_name}}>();
    fromThrift(thrift.value(), proto);
  }
}
void fromThrift(
    const {{class_name}}& thrift,
    std::shared_ptr<facebook::presto::protocol::{{proto_name}}>& proto) {
  {{#fields}}
  if (thrift.getType() == {{class_name}}::Type::{{field_name}}) {
    std::shared_ptr<facebook::presto::protocol::{{#proto_field_type}}{{proto_field_type}}{{/proto_field_type}}{{^proto_field_type}}{{field_type}}{{/proto_field_type}}> {{field_name}};
    fromThrift(thrift.get_{{field_name}}(), {{field_name}});
    proto = {{field_name}};
  }
  {{/fields}}
}

{{/union}}
{{#struct}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{&class_name}}& thrift) {
    {{#fields}}
    toThrift(proto.{{proto_name}}, {{^optional}}*{{/optional}}thrift.{{field_name}}_ref());
    {{/fields}}
}
void fromThrift(const {{&class_name}}& thrift, facebook::presto::protocol::{{#proto_name}}{{proto_name}}{{/proto_name}}{{^proto_name}}{{class_name}}{{/proto_name}}& proto) {
    {{#fields}}
    fromThrift({{^optional}}*{{/optional}}thrift.{{field_name}}_ref(), proto.{{proto_name}});
    {{/fields}}
}

{{/struct}}
{{#wrapper}}
void toThrift(const facebook::presto::protocol::{{class_name}}& proto, {{class_name}}& thrift) {
    {{#fields}}
    toThrift(proto, {{^optional}}*{{/optional}}thrift.{{field_name}}_ref());
    {{/fields}}
}
void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{class_name}}& proto) {
    {{#fields}}
    fromThrift({{^optional}}*{{/optional}}thrift.{{field_name}}_ref(), proto);
    {{/fields}}
}
{{/wrapper}}
{{#enum}}
void toThrift(const facebook::presto::protocol::{{proto_name}}& proto, {{class_name}}& thrift) {
  thrift = ({{class_name}})(static_cast<int>(proto));
}
void fromThrift(const {{class_name}}& thrift, facebook::presto::protocol::{{proto_name}}& proto) {
  proto = (facebook::presto::protocol::{{proto_name}})(static_cast<int>(thrift));
}

{{/enum}}
{{/cinc}}
{{/.}}

}
